/********************************************************/
/*	Copyright (C) 2012 Gong Li Bin			*/
/*	Project:	MapReduce			*/
/*	Author:		gong_libin			*/
/*	Date:		2013_06_08			*/
/*	File:		MrRedTest.cpp			*/
/********************************************************/

#include "MrTask.h"

#define	MR_RT_OFFST			1
#define	MR_RT_TOTAL			4
#define	MR_RT_ADURL			"172.16.8.211:10001"
#define	MR_RT_H1URL			"172.16.8.211:12001"
#define	MR_RT_H2URL			"172.16.8.211:12001"
#define	MR_RT_RDURL			"172.16.8.211:11001"

int main(int argc, char* argv[])
{
	CMrUdp CUdp;
	CMrPro CPro;
	CMrTime CTime;
	int iLength = 0;
	USHORT usTotal = 0;
	char szGlb[] = "I am global";
	char szAdp[MR_URL] = MR_RT_ADURL;
	char szRed[MR_URL] = MR_RT_RDURL;
	char szBuffer[MR_BUFFER] = { 0 };
	UCHAR uszSend[MR_PACKET] = { 0 };
	UCHAR uszRecv[MR_PACKET] = { 0 };
	UCHAR uszLcl1[MR_URL + 1] = { 0 };
	UCHAR uszLcl2[MR_URL + 1] = { 0 };
	UCHAR uszHead[MR_PRO_HEAD] = { 0 };
	MRPROKV_S stKv[] = {
		{MR_MR_ADP, strlen(szAdp), (UCHAR*)szAdp},
		{MR_MR_GLB, strlen(szGlb), (UCHAR*)szGlb},
		{MR_MR_LCL, MR_URL + 1, uszLcl1},
		{MR_MR_LCL, MR_URL + 1, uszLcl2},
	};
	MRPROHEAD_S stHead = {
		MR_PRO_FLAG,
		MR_PRO_1,
		MR_PRO_CTRL,
		MR_PRO_0,
		MR_PRO_Y,
		MR_PRO_0,
		MR_PRO_0,
		MR_PRO_0,
		MR_MR_RD1,
		MR_DEF_SUCC,
		MR_PRO_0,
		MR_PRO_0,
	};

	/* key */
	system("./MrHubIns");
	MR_PRINT("\n");

	/* lcl1 */
	uszLcl1[MR_URL] = '1';
	strcpy((char*)uszLcl1, MR_RT_H1URL);

	/* lcl2 */
	uszLcl2[MR_URL] = '2';
	strcpy((char*)uszLcl2, MR_RT_H2URL);

	/* packet */
	CPro.MrProPutBody(NULL);
	CPro.MrProInduce(&stHead);
	CPro.MrProPutTotal(MR_RT_TOTAL - MR_RT_OFFST);
	CPro.MrProPutLength(MrProKvOutput(stKv + MR_RT_OFFST, MR_RT_TOTAL - MR_RT_OFFST, &uszSend[MR_PRO_HEAD], MR_PACKET - MR_PRO_HEAD));
	CPro.MrProCreate(uszSend, MR_PACKET);

	CTime.MrTimeOn();

	if (MR_SUCCESS == CUdp.MrNetInitSend(szRed)) {
		CUdp.MrNetSetTimeOut(MR_TIMEOUT, 0);
		if (CUdp.MrNetSend(uszSend, MR_PRO_HEAD + CPro.MrProGetLength()) > 0) {
			if (MR_PRO_HEAD == CUdp.MrNetRecv(uszHead, MR_PRO_HEAD)) {
				MR_PRINT("Response:\n");
				CPro.MrProParse(uszRecv, iLength);
				CPro.MrProDisplay();
				if ((iLength = CUdp.MrNetRecv(uszRecv, MR_PACKET)) > 0) {
					/* head */
					MR_PRINT("Result:\n");
					CPro.MrProParse(uszRecv, iLength);
					CPro.MrProDisplay();
					/* body */
					memset(stKv, '\0', sizeof(MRPROKV_S) * MR_RT_TOTAL);
					MrProKvInput(stKv, CPro.MrProGetTotal(), CPro.MrProGetBody(), CPro.MrProGetLength());
					for (usTotal = 0; usTotal < CPro.MrProGetTotal(); usTotal ++) {
						memset(szBuffer, '\0', MR_BUFFER);
						memcpy(szBuffer, stKv[usTotal].m_puszValue, stKv[usTotal].m_iLength);
						MR_PRINT("Return[%d]: %s\n", usTotal, szBuffer + MR_URL);
					}
				}
				else {
					MR_ERROR("Failed to MrNetRecv\n");
				}
			}
			else {
				MR_ERROR("Failed to MrNetRecv\n");
			}
		}
		else {
			MR_ERROR("Failed to MrNetSend\n");
		}
		CUdp.MrNetClose();
	}

	CTime.MrTimeOff();
	CTime.MrTimeDisplay();

	return MR_SUCCESS;
}
